package com.planet.stockanalysis.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.planet.stockanalysis.server.entity.Message;
import com.planet.stockanalysis.server.entity.Message.MessageType;
import com.planet.stockanalysis.server.entity.Request;
import com.planet.stockanalysis.server.entity.Stock;
import com.planet.stockanalysis.server.entity.User;
import com.planet.stockanalysis.server.service.StockService;
import com.planet.stockanalysis.server.service.UserService;

@Service("controller")
public class Controller {
	@Autowired
	StockService stockService;
	@Autowired
	UserService userService;

	public enum Opiton{
		/**
		 * 获取股票信息操作
		 */
		stock,
		login
	}

	public Message dispose(Request request){
		Message msg = new Message();
		User user;
		switch (request.option) {
		case login:
			user = userService.login(request.name, request.password);
			if(user != null){
				msg.type = MessageType.validated;
				msg.value = "登录成功";
			}else{
				msg.type = MessageType.message;
				msg.value = "用户名或密码错误";
			}
			break;
		case stock:
			user = userService.login(request.name, request.password);
			if(user == null){
				msg.type = MessageType.message;
				msg.value = "用户名或密码错误";
			}else{
				List<Stock> stocks = stockService.getAll();
				msg.type = MessageType.data;
				msg.value = new Gson().toJson(stocks);
			}
			break;
		default:
			msg.type = MessageType.message;
			msg.value = "未处理请求";
			break;
		}
		return msg;
	}
}
